<?php
class LoginReg extends MY_Controller
{

    protected $__enable_login_check = false;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('services/user_service', 'user_service');
    }

    public function getVcode()
    {
        $user_phone = $this->request_array['phone'] ?? null;
        $err = null;
        $vcode = $this->user_service->getVcode($user_phone, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode']);
        }
        $this->renderJson(STATUS_OK, ['vcode' => $vcode]);
    }

    public function login()
    {
        $user_phone = $this->request_array['phone'] ?? null;
        $hash_pwd = $this->request_array['hashPwd'] ?? null;
        $err = null;
        $result = $this->user_service->login($user_phone, $hash_pwd, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $result);
    }

    public function sendSms()
    {
        $phone = $this->request_array['phoneNumber'] ?? '';
        $err = null;
        $res = $this->user_service->sendSms($phone, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson();
    }

    public function reg()
    {
        $phone = $this->request_array['phoneNumber'] ?? '';
        $code = $this->request_array['code'] ?? '';
        $password = $this->request_array['password'] ?? '';
        $err = null;
        $res = $this->user_service->checkRegSmsCode($phone, $code, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $reg = $this->user_service->register($phone, $password, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $reg);
    }
}